/**
 * Created by dqw on 2015/12/30.
 */

//列表开始
var dtGridColumns = [
    {
        id: 'delFlag',
        type: 'int',
        hideQuery: true,
        'export': false,
        hideQueryType: 'eq',
        hideQueryValue: 1,
        hide: true
    },
    {
        id: 'id',
        title: 'ID',
        type: 'number',
        columnClass: 'text-center width-20',
        fastSort: false
    },
    {
        id: 'holiday',
        title: '日期',
        type: 'string',
        columnClass: 'text-center width-20',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'eq'
    },
    {
        id: 'createTime',
        title: '添加日期',
        type: 'date',
        format: 'yyyy-MM-dd',
        columnClass: 'text-center width-300',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'range'
    },
    {
        id: 'creator',
        title: '添加人',
        type: 'string',
        columnClass: 'text-center width-300',
    },
    {
        id: 'remark',
        title: '备注',
        type: 'string',
        columnClass: 'text-center width-300',
        fastQuery: true,
        fastQueryType: 'eq'
    },
    {
        id: 'operation',
        title: '管理操作',
        type: 'string',
        columnClass: 'text-center width-400',
        fastSort: false,
        extra: false,
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var html = "",
                del, edit;
            if ($("#holidayEdit") && $("#holidayEdit").val() == 1) {
                edit = "<a data-target='#editModal' class='btn btn-sm btn-primary m-r-10' data-toggle='modal' data-no='" + dataNo + "' ><i class='fa fa-edit'></i>&nbsp;编辑&nbsp;</a>";
                html += edit;
            }
            if ($("#holidayDel") && $("#holidayDel").val() == 1) {
                del = "<a href='javascript:;' class='btn btn-danger btn-sm m-r-10' onclick='OperateHandle.delInfo(" + record.id + ",\"" + record.holiday + "\")'><i class='fa fa-trash-o'></i>&nbsp;删除&nbsp;</a>";
                html += del;
            }
            return html;
        }
    }
];

var dtGridOption = {
    lang: 'zh-cn',
    ajaxLoad: true,
    loadURL: ncGlobal.adminRoot + 'holiday/json/list',
    columns: dtGridColumns,
    gridContainer: 'dtGridContainer',
    toolbarContainer: 'dtGridToolBarContainer',
    pageSize: 10,
    pageSizeLimit: [10, 20, 50],
    ncColumnsType: {int: ["id", "userId"], Timestamp: ["createTime"]}
};

var grid = $.fn.DtGrid.init(dtGridOption);
grid.fastQueryParameters = new Object();
//排序
grid.sortParameter.columnId = ['asc_id'];
grid.fastQueryParameters['eq_delFlag'] = 1;
grid.sortParameter.sortType = 1;
//列表结束

//操作处理开始
var OperateHandle = function () {

    function _bindEvent() {
        //新增对话框初始化
        $("#addModal").on("show.bs.modal", function (event) {
            //清除错误信息
            $(".alert-danger").remove();
            $("#addForm").psly().reset();

            $("#addForm").find("[name='holiday']").val("");
            $("#addForm").find("[name='remark']").val("");
        });

        //模糊搜索
        $('#customSearch').click(function () {
            grid.fastQueryParameters = new Object();
            grid.fastQueryParameters['eq_delFlag'] = 1;
            grid.fastQueryParameters['lk_name'] = $('#keyword').val();
            grid.pager.startRecord = 0;
            grid.pager.nowPage = 1;
            grid.pager.recordCount = -1;
            grid.pager.pageCount = -1;
            grid.refresh(true);
        });
    }

    $('#editModal').on('show.bs.modal', function (event) {
        //获取接受事件的元素
        var button = $(event.relatedTarget),
            //获取data 参数
            datano = button.data('no'),
            modal = $(this),
            //获取列表框中的原始数据
            gridData = grid.sortOriginalDatas[datano],
            editForm = $("#editForm");
        //清除错误提示
        modal.find('input[name="id"]').val(gridData.id);
        modal.find('input[name="holiday"]').val(gridData.holiday);
        modal.find('input[name="remark"]').val(gridData.remark);
    });

    /**
     * 删除
     */
    function _delInfo(id, content) {
        var tpl = '您选择对 <strong>' + content + '</strong> 进行删除操作，删除后将无法恢复。<br/>您确定要进行删除操作吗?'
        $.ncConfirm({
            url: ncGlobal.adminRoot + "holiday/json/del",
            data: {
                id: id
            },
            content: tpl,
            alertTitle: "删除操作"
        });
    }

    //预览
    $('#previewModal').on('show.bs.modal', function (event) {
        var    //获取接受事件的元素
            button = $(event.relatedTarget),
            //获取data 参数
            datano = button.data('no'),
            modal = $(this),
            //获取列表框中的原始数据
            gridData = grid.sortOriginalDatas[datano],
            editForm = $("#previewForm");
        modal.find('input[name="id"]').val(gridData.id);
        modal.find('input[name="title"]').val(gridData.title);
        modal.find('input[name="publisher"]').val(gridData.publisher);
        if (gridData.content != null) {
            OperateHandle.ueEdit.setContent(gridData.content);
        }
        //清除错误提示
        editForm.psly().reset();
        $(".alert-danger").remove()
    });

    /**
     * 删除入驻申请
     */
    function _delAdmin(id) {
        var tpl = '您确定要删除该管理员吗?'
        $.ncConfirm({
            url: ncGlobal.adminRoot + "admin/del.json",
            data: {
                id: id
            },
            content: tpl
        });
    }

    //外部可调用
    return {
        bindEvent: _bindEvent,
        delAdmin: _delAdmin,
        // ueAdd: _ueAdd,
        // ueEdit: _ueEdit,
        delInfo: _delInfo
    }
}();
//操作处理结束

$(function () {
    //加载列表
    grid.load();
    //页面绑定事件
    OperateHandle.bindEvent();
    //实例化编辑器
    // OperateHandle.ueAdd = UE.getEditor('contnetAdd', {textarea: 'content'});
    // OperateHandle.ueEdit = UE.getEditor('contnetEditor', {textarea: 'content'});
    // OperateHandle.ueEdit = UE.getEditor('contnetpreview', {textarea: 'content'});

});